/*
 * strncat_s.c - Safe bounded string concatenation with bounds checking
 */

#include "bounds_check.h"
#include "bounds_check_internal.h"
#include <string.h>

int strncat_s(char *dest, size_t dest_size, const char *src, size_t count) {
    if (!is_valid_buffer(dest, dest_size) || src == NULL) {
        return BOUNDS_CHECK_INVALID_PARAM;
    }

    /* Find end of destination string */
    size_t dest_len = 0;
    while (dest[dest_len] != '\0' && dest_len < dest_size) {
        dest_len++;
    }

    if (dest_len >= dest_size) {
        return BOUNDS_CHECK_INVALID_PARAM;
    }

    /* Calculate remaining space */
    size_t remaining = dest_size - dest_len - 1;
    size_t copy_len = (count < remaining) ? count : remaining;

    /* Copy source string */
    size_t i;
    for (i = 0; i < copy_len && src[i] != '\0'; i++) {
        dest[dest_len + i] = src[i];
    }

    /* Null terminate */
    dest[dest_len + i] = '\0';

    return BOUNDS_CHECK_SUCCESS;
}